/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package formatconversion_model;

import java.util.ArrayList;
import magic_model.Card;
import magic_model.Deck;
import magic_model.MalformedFileException;

/**
 *
 * @author Tobias
 */
public class Cockatrice_Parser implements MagicFormatParser {
    private boolean isMain = true;
    
    public Deck importFile(String[] lines) {
        Deck deck = new Deck();
        for(int i=0;i<lines.length;i++){
            parseLine(lines[i],deck);
        }
        deck.sort();
        return deck;
    }

    private void parseLine(String line,Deck deck) {
        if(line.contains("<card")){
          if(!lineCorrect(line))throw new MalformedFileException("File is not in Cockatrice - Format");
         //  <card number="4" price="0" name="Azorius Charm"/>
          int indexOfNumber=line.indexOf("number=");
          int indexOfName=line.indexOf("name=");
          int endOfName = line.indexOf("\"",indexOfName+6);
          if(indexOfNumber==-1 || indexOfName==-1 || endOfName==-1 ) throw new MalformedFileException("File is not in Cockatrice - Format");
          int quantity = Integer.parseInt(line.substring(indexOfNumber+9,line.indexOf("\"",indexOfNumber+9)));
          String name =line.substring(indexOfName+6,endOfName);
          deck.addCard(new Card(quantity,name,isMain));
        }else{
            if(line.contains("<zone name=\"side\">")){
                isMain=false;
            }else if (line.contains("<deckname>")){
                deck.setName(line.substring(line.indexOf("<deckname>")+9,line.indexOf("</deckname>")));
            }
        }
    }

    private boolean lineCorrect(String line){
        return true;
    }
    
    public String[] exportFile(Deck intern) {
        intern.sort();
        ArrayList<String> content = new ArrayList<String>();
        content.add("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        content.add("<cockatrice_deck version=\"1\">");
        content.add("\t<deckname>"+intern.getName()+"</deckname>");
        content.add("\t<comments>generated with Tobis Toolbox</comments>");
        content.add("\t<zone name=\"main\">");
        
        boolean isMain=true;
        for(Card c : intern.getCards()){
          if(c.isMain()==false && isMain==true){
                content.add("\t</zone>");
                content.add("\t<zone name=\"side\">");
                isMain=false;
          }
            content.add("\t\t<card number=\""+c.getQuantity()+"\" price=\"0\" name=\""+c.getName()+"\"/>");
            //wenn 60 karten gefunden wurd, aber nur einmal, und nur wenn noch mehr zeilen kommen, wird das sideboard erstellt

        }
        if(isMain){
            content.add("\t</zone>");
        }

       content.add("</cockatrice_deck>");
       return content.toArray(new String[content.size()]);
    }

  @Override
    public String toString(){
        return "Cockatrice";
    }
    
    public FileType getFileType(){
        return FileType.cod;
    }

}
